<?php


use models\Business\Alibaba;
use models\Exception\Business\AddressException;
use Yaf\Registry;
use models\Business\Address;
use ylAlibaba\core\ylAlibabaException;

class AddressController extends ApiBaseController
{
    /**
     * @SWG\Post(
     *     path="/v3/address/create",
     *     tags={"收货地址"},
     *     summary="创建收货地址接口",
     *     description="
     * province 必须参数 省份名称
     * city 必须参数 城市名称
     * area 必须名称 区县名称
     * street 非必须参数 街道名称
     * address 必须参数 详细地址
     * name 必须参数 收货人姓名
     * mobile 必须参数 收货人手机号
     * is_default 必须参数 是否是默认地址 0为否，1为是
     * zipcode 非必须参数 邮政编码",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="201",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/addressSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="创建失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function createAction() {
        $parameters = Registry::get('parameters');

        $address = Address::createAddress($parameters);
        try {
            Alibaba::getTradeAddressCodeParse($address['id']);
        } catch (ylAlibabaException $e) {
            Registry::get('alibaba_log')->err("阿里巴巴地址解析报错: " . $e->getMessage());
            throw new AddressException(AddressException::ADDRESS_ERROR, AddressException::ADDRESS_ERROR_NO);
        }
        return $this->_responseJson(['data' => $address]);
    }

    /**
     * @SWG\Get(
     *     path="/v3/address/list",
     *     tags={"收货地址"},
     *     summary="获取收货地址列表数据接口",
     *     description="
     * page 必须参数 页码
     * page_size 必须参数 每页显示条数
     * sort 必须参数 排序方式 可选值:desc降序、asc生序
     * order 必须参数 排序字段 固定值 id
     *     ",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/addressListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function listAction() {
        $parameters = Registry::get('parameters');

        $address_list = Address::getAddressList($parameters);

        return $this->_responseJson($address_list);
    }

    /**
     * @SWG\Put(
     *     path="/v3/address/{id}/edit",
     *     tags={"收货地址"},
     *     summary="编辑收货地址接口",
     *     description="
     * province 非必须参数 省份名称 修改省份时必须
     * city 非必须参数 城市名称 修改城市时必须
     * area 非必须名称 区县名称 修改区县时必须
     * street 非必须参数 街道名称 修改街道名称时必须
     * address 非必须参数 详细地址 修改详细地址时必须
     * name 非必须参数 收货人姓名 修改收货人姓名时必须
     * mobile 非必须参数 收货人手机号 修改收货人手机号时必须
     * is_default 非必须参数 是否是默认地址 0为否，1为是 修改默认地址时必须
     * zipcode 非必须参数 邮政编码 修改邮政编码时必须",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="/admin/Address/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/addressSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function editAction() {
        $parameters = Registry::get('parameters');

        $address = Address::editAddress($parameters);
        try {
            Alibaba::getTradeAddressCodeParse($address['id']);
        } catch (ylAlibabaException $e) {
            Registry::get('alibaba_log')->err("阿里巴巴地址解析报错: " . $e->getMessage());
            throw new AddressException(AddressException::ADDRESS_ERROR, AddressException::ADDRESS_ERROR_NO);
        }
        return $this->_responseJson(['data' => $address]);
    }

    /**
     *
     * @SWG\Get(
     *     path="/v3/address/{id}/info",
     *     tags={"收货地址"},
     *     summary="获取指定编号的收货地址",
     *     description="
     * timestamp 必须参数 当前时间戳",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="收货地址编号 通过address/list接口获取",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="所有参数使用&连接的rsa加密结果",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/addressSingleData")
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="签名不存在或无效签名"
     *     )
     * )
     */
    public function infoAction() {
        $parameters = Registry::get('parameters');
        $parameters['stats'] = 1;

        $address = Address::getAddress($parameters);

        return $this->_responseJson(['data' => $address]);
    }

    /**
     * @SWG\Delete(
     *     path="/v3/address/{id}/delete",
     *     tags={"收货地址"},
     *     summary="删除收货地址接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="/v3/address/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="删除成功, 没有返回结果"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function deleteAction() {
        $parameters = Registry::get('parameters');

        Address::editAddress(['id' => $parameters['id'], 'stats' => 0]);

        return $this->_responseJson(new stdClass(), '删除成功', '000');
    }

}
